/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.tedu.mall.sso.service.impl;

import cn.tedu.mall.sso.pojo.domain.SendParam;
import com.tarena.mnmp.api.TargetDTO;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service("mnmpCode")
@Slf4j
public class MnmpCodeServiceImpl extends AbstractCodeService {
    private static final RestTemplate REST_TEMPLATE = new RestTemplate();
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Value("${sms.url}")
    private String smsUrl;

    public void send(String phoneNum, String code) {
        /*send.app.code=JSDTN2208
        send.temp.content=${msg}
        send.trigger.pattern=yyyy-MM-dd HH:mm
        send.provider.code=HW_SMS*/
        log.info("验证码:{}", code);
        //发送验证码到手机
        SendParam sendParam = new SendParam();
        sendParam.setAppCode("bm_01");
        sendParam.setTemplateCode("COOL_TEMPLATE05");
        sendParam.setSignCode("TEDU_TC");
        sendParam.setMock(0);
        sendParam.setProviderCode("HW_SMS");
        Map<String, Object> params = new HashMap<>();
        params.put("msg", code);
        //sendParam.setTaskId(-19999L);
        TargetDTO targetDTO = new TargetDTO(phoneNum, params);
        List<TargetDTO> targetDTOS = new ArrayList<>();
        targetDTOS.add(targetDTO);
        sendParam.setTargets(targetDTOS);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd HH:MM");
        String triggertime = LocalDateTime.now().format(formatter).toString();
        sendParam.setTriggerTime(triggertime);
        REST_TEMPLATE.postForObject(smsUrl, sendParam, String.class);
    }

    @Override public void sendCode(String phoneNum, String validCode, String validCodeId, String ip) {
        super.setRedisTemplate(redisTemplate);
        validatePhone(phoneNum);
        checkValidCode(validCode, validCodeId, ip);
        lockCheck(phoneNum);
        String code = generateCode(phoneNum);
        //验证一下validcodeId validCode
        send(phoneNum, code);
    }
}
